<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import * as echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from '../../dashboard/mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '200px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions(this.chartData)
    },
    setOptions({ expectedData, actualData, title } = {}) {
      var option = {
        color: '#3BA1FF',
        barWidth: '10%',
        tooltip: {
          trigger: 'axis'
        },
        grid: {
          left: '5%',
          right: '0',
          bottom: '30%',
          top: '10%',
        },
        legend: {
          show: false,
        },
        label:{
          show:true,
          position:'top',		//设置的位置
          textStyle:{
            color:'#1cd0c3',	//文字的颜色
            fontSize:18,		//字体大小
            fontWeight:'bold',	//设置粗体
          }
        },
        xAxis: {
          type: 'category',
          data: actualData,
        },
        yAxis: {
          type: 'value',
          // min:0,
          // max:100,
        },
        series: [
          {
            name: '数据时延数字',
            type: 'bar',
            data: expectedData,
          }
        ]
      };
      this.chart.setOption(option)
    }
  }
}
</script>
